package capricornus.components.utils.colorsparkle
{
	import capricornus.components.utils.color.Color;
	
	import fl.transitions.Tween;
	import fl.transitions.TweenEvent;
	import fl.transitions.easing.Regular;
	
	import flash.display.DisplayObject;
	import flash.events.EventDispatcher;
	import flash.geom.ColorTransform;

	/**
	 * 元件彩色变幻类
	 * @author kael
	 * 
	 */	
	public class ColorSparkleController extends EventDispatcher
	{
		private var _target:DisplayObject;
		
		private var _toColor:Color;
		private var _colorTransform:ColorTransform;
		
		private var tween:Tween;
		
		private var _sparkleEnd:Number = 0.8;
		
		public function ColorSparkleController()
		{
			init();
		}
		
		private function init():void
		{
			_toColor = Color.getRGB( 0xffffff );
			_colorTransform = new ColorTransform( 0, 0, 0, 1 );
			
			tween = new Tween( this, "color", Regular.easeIn, 0, 0, 1, true );
			tween.looping = false;
			tween.addEventListener( TweenEvent.MOTION_FINISH, onMotionFinish, false, 0, true );
			tween.stop();
		}
		
		public function destroy():void
		{
			tween.removeEventListener( TweenEvent.MOTION_FINISH, onMotionFinish );
			tween = null;
			_colorTransform = null;
			_target = null;
		}
		
		public function isPlaying():Boolean
		{
			return tween.isPlaying;
		}
		
		public function set color( value:Number ):void
		{
			if( _target==null ) return;
			
			_colorTransform.redMultiplier = (1-value);
			_colorTransform.greenMultiplier = (1-value);
			_colorTransform.blueMultiplier = (1-value);
			
			_colorTransform.redOffset = _toColor.r * value;
			_colorTransform.greenOffset = _toColor.g * value;
			_colorTransform.blueOffset = _toColor.b * value;
			
			_target.transform.colorTransform = _colorTransform;
		}
		
		private function onMotionFinish( e:TweenEvent ):void
		{
			if( _target==null ) return;
			
			if( tween.finish==_sparkleEnd )
			{
				this.dispatchEvent( new ColorSparkleEvent( ColorSparkleEvent.SPARKLE_COMPLETE ) );
			}
			else
			{
				this.dispatchEvent( new ColorSparkleEvent( ColorSparkleEvent.FADE_COMPLETE ) );
			}
			
			_colorTransform.redMultiplier = 1;
			_colorTransform.greenMultiplier = 1;
			_colorTransform.blueMultiplier = 1;
			_colorTransform.redOffset = 0;
			_colorTransform.greenOffset = 0;
			_colorTransform.blueOffset = 0;
			_target.transform.colorTransform = _colorTransform;
			
			tween.stop();
		}
		
		public function attackTarget( target:DisplayObject ):void
		{
			_target = target;
		}
		
		public function sparkle( time:Number=1.2 ):void
		{
			tween.begin = 0;
			tween.finish = _sparkleEnd;
			tween.duration = time;
			
			_colorTransform.redMultiplier = 1;
			_colorTransform.greenMultiplier = 1;
			_colorTransform.blueMultiplier = 1;
			_colorTransform.redOffset = 0;
			_colorTransform.greenOffset = 0;
			_colorTransform.blueOffset = 0;
			
			tween.start();
		}
		
		public function fade( time:Number=1.2 ):void
		{
			tween.begin = _sparkleEnd;
			tween.finish = 0;
			tween.duration = time;
			
			tween.start();
		}
	}
}